layui.define(['table', 'form', 'layer', 'setter', 'view', 'admin'], function (exports) {
    var $ = layui.$,
        admin = layui.admin,
        view = layui.view,
        table = layui.table,
        form = layui.form,
        layer = layui.layer,
        router = layui.router(),
        setter = layui.setter;
    var token = 'EEAADD2299AAEE226677331177885599'

    // 查询功能
    $('#search-custom').click(function (res) {
        var name = $('#search-input').val();
        renderT(name);
    })

    var renderT = function (name) {
        table.render({
            elem: '#customTable',
            page: true,
            url: 'http://www.iotddc.com/IOTDDC/customreport/bemreportPaging',
            method: 'post',
            // even: true,
            toolbar: '#toolbar', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: [],
            where: {
                token: token,
                name: name
            },
            request: {
                pageNmae: 'pageindex',
                limitName: 'pagesize'
            },
            parseData:function(res){
                return {
                    "data":res.Datas,
                    "count":res.paging ? res.paging.count : 0,
                    "code":res.code,
                    "msg":res.message ? res.message : ""
                }
            },
            response: {
                statusName: 'code',
                statusCode: 200,
            },
            cols: [
                [{
                        type: 'checkbox'
                    },
                    {
                        field: 'name',
                        title: '报表名称',
                        align: 'center'
                    },
                    {
                        title: '报表设计',
                        align: 'center',
                        toolbar: '#report-design'
                    }, {
                        title: '报表查看',
                        align: 'center',
                        toolbar: '#report-check'
                    }
                ]
            ]
        })
    }

    renderT(null);

    table.on('toolbar(customTable)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id)
        // 增加
        if (obj.event === 'report-add') {
            admin.popup({
                title: '新增自定义报表',
                area: ['400px', '230px'],
                id: 'LAY-custom-popup-add',
                success: function (layero, index) {
                    view(this.id).render('report/customPopup-add', {
                            type: "add"
                        })
                        .done(function () {
                            form.render(null, 'add-form')
                            form.on('submit(add-submit)', function (obj) {
                                var field = obj.field;
                                admin.req({
                                    url: 'http://www.iotddc.com/IOTDDC/customreport/addCustomreport',
                                    method: 'post',
                                    data: {
                                        name: field.name,
                                        token: token
                                    },
                                    done: function (res) {
                                        layui.table.reload('customTable'); //重载表格
                                        layer.msg(res.message)
                                        layer.close(index)
                                    }
                                })
                            })
                        })
                }
            })
        }
        // 修改
        else if (obj.event === 'report-edit') {
            if (checkStatus.data.length === 0) {
                layer.msg('请选择需要修改的数据!')
            } else if (checkStatus.data.length === 1) {
                //可以修改  
                admin.popup({
                    title: '修改报表',
                    area: ['400px', '230px'],
                    id: 'LAY-custom-popup-edit',
                    success: function (layero, index) {
                        view(this.id).render('report/customPopup-add', {
                                type: "edit",
                                name: checkStatus.data[0].name
                            })
                            .done(function () {
                                form.render(null, 'add-form')
                                form.on('submit(add-submit)', function (obj) {
                                    var field = obj.field;
                                    var id = checkStatus.data[0].id
                                    admin.req({
                                        url: 'http://www.iotddc.com/IOTDDC/customreport/Updatereport',
                                        method: 'post',
                                        data: {
                                            name: field.name,
                                            id: id,
                                            token: token,
                                            remark: null
                                        },
                                        done: function (res) {
                                            layui.table.reload('customTable'); //重载表格
                                            if (res.code === '200') {
                                                layer.msg('修改成功')
                                            } else {
                                                layer.msg('修改失败')
                                            }
                                            layer.close(index)
                                        }
                                    })
                                })
                            })
                    }
                })
            } else {
                layer.msg('每次只能对一条数据进行修改!')
            }
        } else if (obj.event === 'report-del') {
            if (checkStatus.data.length === 0) {
                layer.msg('请选择要删除的数据')
            } else {
                layer.confirm('确定要删除报表吗？', {
                    btn: ['删除', '取消']
                }, function () {
                    var mid = ''
                    layui.each(checkStatus.data, function (index, item) {
                        mid += item.id + ','
                    });
                    mid = mid.slice(0, -1);
                    admin.req({
                        url: 'http://www.iotddc.com/IOTDDC/customreport/delCustomreport',
                        method: 'post',
                        data: {
                            token: token,
                            mid: mid
                        },
                        done: function (res) {
                            layui.table.reload('customTable'); //重载表格
                            layer.msg(res.message)
                        }
                    })
                });
            }
        }
    })

    // 点击报表设计
    table.on('tool(customTable)', function (obj) {
        if (obj.event === 'report-design') {
            location.hash = '/report/customPopup-design'
        }else if (obj.event === 'report-check'){
            location.hash = '/report/customPopup-check'

        }
        // 存储此条数据
        layui.data('customItem-data', {
            key: 'data',
            value: obj.data
        })
    })







    exports('custom', {});
});